package com.cheng.admin.controller;


import cn.dev33.satoken.annotation.SaCheckRole;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cheng.admin.model.UnitPrice;
import com.cheng.admin.service.UnitPriceService;
import com.cheng.common.domain.BaseSearchVO;
import com.cheng.common.domain.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 单位价格 前端控制器
 * </p>
 * 照片价格，字数价格，抽成价格，佣金价格
 *
 * @author 起凡
 * @since 2022-01-17
 */
@RestController
@RequestMapping("/unit-price")
public class UnitPriceController {
    @Autowired
    UnitPriceService unitPriceService;

    @GetMapping("get")
    public R<UnitPrice> get(@RequestParam Long id) {
        return R.ok(unitPriceService.getById(id));
    }

    @SaCheckRole("admin")
    @PostMapping("save")
    public R<Boolean> save(@RequestBody @Validated UnitPrice unitPrice) {
        unitPriceService.saveOrUpdate(unitPrice);
        return R.ok(true);
    }

    @GetMapping("list")
    public R<PageInfo<UnitPrice>> list(BaseSearchVO baseSearchVO) {
        PageHelper.startPage(baseSearchVO.getPageNum(), baseSearchVO.getPageSize());
        LambdaQueryWrapper<UnitPrice> queryWrapper = new LambdaQueryWrapper<>();
        if (baseSearchVO.getType() != null) {
            queryWrapper.eq(UnitPrice::getType, baseSearchVO.getType());
        }
        queryWrapper.orderByAsc(UnitPrice::getFirstPrice);
        List<UnitPrice> list = unitPriceService.list(queryWrapper);
        PageInfo<UnitPrice> pageInfo = new PageInfo<>(list);
        return R.ok(pageInfo);
    }

    @SaCheckRole("admin")
    @GetMapping("delete")
    public R<Boolean> delete(@RequestParam Long id) {
        unitPriceService.removeById(id);
        return R.ok(true);
    }
}

